class Solution {
public:
    int findLHS(vector<int>& nums) {
        
        unordered_map<int,int> has;
        int ret=0;
        for(auto &item:nums){

            has[item]++;
        }

        for(int i=0;i<nums.size();++i){

            if(has.count(nums[i]+1)){

                ret=max(ret,has[nums[i]]+has[nums[i]+1]);
            }
            if(has.count(nums[i]-1)){

                ret=max(ret,has[nums[i]]+has[nums[i]-1]);
            }
        }
        
        return ret;
    }
};
